<?php

namespace SisesporteAdmin\Controller;

use Zend\Mvc\Controller\AbstractActionController,
    Zend\View\Model\ViewModel;

abstract class CrudController extends AbstractActionController
{

    /**
     * @var EntityManager
     */
    protected $em;
    protected $service;
    protected $entity;
    protected $form;
    protected $route;
    protected $controller;

    public function indexAction ()
    {
        $data = $this->getEm()
                ->getRepository($this->entity)
                ->findAll();
        return new ViewModel(array('data' => $data, 'flashMessenger' => $this->flashMessenger()->getMessages()));
    }

    public function newAction ()
    {
        $form = new $this->form();

        $request = $this->getRequest();

        if ($request->isPost()) {
            $array = $request->getPost();
            $array['stAtivo'] = TRUE;
            $form->setData($array);
            if ($form->isValid()) {
                $service = $this->getServiceLocator()->get($this->service);

                if ($service->insert($request->getPost()->toArray())) {
                    $this->flashMessenger()->addMessage("Salvo com Sucesso!")->addInfoMessage('success');
                    return $this->redirect()->toRoute($this->route, array('controller' => $this->controller));
                } else {
                    $this->flashMessenger()->addMessage("Falha ao Salvar!")->addInfoMessage('error');
                    return $this->redirect()->toRoute($this->route, array('controller' => $this->controller,
                                'action' => 'new'));
                }
            }
        }

        return new ViewModel(array('form' => $form, 'flashMessenger' => $this->flashMessenger()->getMessages()));
    }

    /**
     * 
     * @return \Zend\View\Model\ViewModel
     */
    public function editAction ()
    {
        $form = new $this->form();
        $request = $this->getRequest();

        $repository = $this->getEm()->getRepository($this->entity);
        $entity = $repository->find($this->params()->fromRoute('id', 0));

        if ($this->params()->fromRoute('id', 0)) {
            $array = $entity->toArray();
            $array['stAtivo'] = TRUE;
            $form->setData($array);
        }

        if ($request->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
                $service = $this->getServiceLocator()->get($this->service);
                $service->update($this->params()->fromRoute('id', 0), $request->getPost()->toArray());

                return $this->redirect()->toRoute($this->route, array('controller' => $this->controller));
            }
        }

        return new ViewModel(array('form' => $form, 'entity' => $entity->toArray()));
    }

    /**
     * Deletar
     * @return redirect
     */
    public function deleteAction ()
    {
        $service = $this->getServiceLocator()->get($this->service);
        if ($service->delete($this->params()->fromRoute('id', 0)))
            return $this->redirect()->toRoute($this->route, array('controller' => $this->controller));
    }

    /*
     * @return EntityManager
     */

    protected function getEm ()
    {
        if (null === $this->em)
            $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');

        return $this->em;
    }

}